﻿using System.Drawing;
using ConsoleSample.Models;
using OfficeOpenXml;

namespace ConsoleSample.Tools.Exports
{
    internal sealed class InvoiceExportHelper
    {
        private readonly List<InvoiceInfo> _infoList;
        private readonly Font _titleFont;
        private readonly Font _displayFont;

        public InvoiceExportHelper(List<InvoiceInfo> infoList)
        {
            _infoList = new List<InvoiceInfo>();
            _infoList.AddRange(infoList);

            _titleFont = new Font("新宋体", 12, FontStyle.Bold);
            _displayFont = new Font("新宋体", 12, FontStyle.Regular);
        }

        public void ExportExcel(string filePath, string sheetName = "Sheet1")
        {
            using var stream = File.Create(filePath);
            using var package = new ExcelPackage(stream);
            var worksheet = package.Workbook.Worksheets.Add(sheetName);
            InitExcelSize(package, worksheet); //初始化单元格宽高
            SetPaperSetInfo(worksheet); //设置试卷信息
            SetResultInfo(worksheet); //设置成绩信息
            package.Save();
        }

        //初始化单元格宽高
        private void InitExcelSize(ExcelPackage package, ExcelWorksheet worksheet)
        {
            //12列
            worksheet.Column(1).Width = 30;
            worksheet.Column(2).Width = 30;
            worksheet.Column(3).Width = 30;
            worksheet.Column(4).Width = 30;
            worksheet.Column(5).Width = 30;
            worksheet.Column(6).Width = 30;
            worksheet.Column(7).Width = 30;
            worksheet.Column(8).Width = 30;
            worksheet.Column(9).Width = 30;
            worksheet.Column(10).Width = 30;
            worksheet.Column(11).Width = 30;
            worksheet.Column(12).Width = 30;

            //行
            for (var i = 0; i < _infoList.Count; i++)
            {
                worksheet.Row(i + 1).Height = 20;
            }

            package.Workbook.Calculate();
        }

        private void SetPaperSetInfo(ExcelWorksheet worksheet)
        {
            EpPlusHelper.SetCellValueWithoutFill(worksheet, _titleFont, 1, 1, "发票号码");
            EpPlusHelper.SetCellValueWithoutFill(worksheet, _titleFont, 1, 2, "开票日期");
            EpPlusHelper.SetCellValueWithoutFill(worksheet, _titleFont, 1, 3, "合计金额");
            EpPlusHelper.SetCellValueWithoutFill(worksheet, _titleFont, 1, 4, "合计税额");
            EpPlusHelper.SetCellValueWithoutFill(worksheet, _titleFont, 1, 5, "价税合计（大写）");
            EpPlusHelper.SetCellValueWithoutFill(worksheet, _titleFont, 1, 6, "价税合计（小写）");
            EpPlusHelper.SetCellValueWithoutFill(worksheet, _titleFont, 1, 7, "购买方名称");
            EpPlusHelper.SetCellValueWithoutFill(worksheet, _titleFont, 1, 8, "购买方统一社会信用代码/纳税人识别号");
            EpPlusHelper.SetCellValueWithoutFill(worksheet, _titleFont, 1, 9, "销售方名称");
            EpPlusHelper.SetCellValueWithoutFill(worksheet, _titleFont, 1, 10, "销售方统一社会信用代码/纳税人识别号");
            EpPlusHelper.SetCellValueWithoutFill(worksheet, _titleFont, 1, 11, "开票人");
            EpPlusHelper.SetCellValueWithoutFill(worksheet, _titleFont, 1, 12, "备注");
        }

        private void SetResultInfo(ExcelWorksheet worksheet)
        {
            var row = 1;
            foreach (var info in _infoList)
            {
                row++;
                EpPlusHelper.SetCellValueWithoutFill(worksheet, _displayFont, row, 1, info.InvoiceNum);
                EpPlusHelper.SetCellValueWithoutFill(worksheet, _displayFont, row, 2, info.InvoiceDate);
                EpPlusHelper.SetCellValueWithoutFill(worksheet, _displayFont, row, 3, info.TotalAmount);
                EpPlusHelper.SetCellValueWithoutFill(worksheet, _displayFont, row, 4, info.TotalTax);
                EpPlusHelper.SetCellValueWithoutFill(worksheet, _displayFont, row, 5, info.AmountInWords);
                EpPlusHelper.SetCellValueWithoutFill(worksheet, _displayFont, row, 6, info.AmountInFigures);
                EpPlusHelper.SetCellValueWithoutFill(worksheet, _displayFont, row, 7, info.PurchaserName);
                EpPlusHelper.SetCellValueWithoutFill(worksheet, _displayFont, row, 8, info.PurchaserRegisterNum);
                EpPlusHelper.SetCellValueWithoutFill(worksheet, _displayFont, row, 9, info.SellerName);
                EpPlusHelper.SetCellValueWithoutFill(worksheet, _displayFont, row, 10, info.SellerRegisterNum);
                EpPlusHelper.SetCellValueWithoutFill(worksheet, _displayFont, row, 11, info.NoteDrawer);
                EpPlusHelper.SetCellValueWithoutFill(worksheet, _displayFont, row, 12, info.Remarks);
            }
        }
    }
}
